//
//  ByUIForgetVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIForgetVC.h"
#import "ByLoginUserOperationView.h"

// api
#import "ByApiUser.h"

@interface ByUIForgetVC ()<UITableViewDelegate,UITableViewDataSource>
// view
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) ByLoginUserOperationView *userView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIButton *getCodeBtn;


// data
@property (nonatomic, assign) NSInteger countdownTime;   // 倒计时剩余时间
@end

@implementation ByUIForgetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    [self addLeftBackBarButtonItem];
    [self initTableView];
}

#pragma mark -- 创建视图
- (void)initTableView{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.delegate = self;
    _table.dataSource = self;
    _table.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_table];
    
    _table.tableFooterView = self.footerView;
}

#pragma mark -- 懒加载
-(ByLoginUserOperationView *)userView{
    if (!_userView) {
        _userView = [[ByLoginUserOperationView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, LineHeight *4 + 120 + 4* 30) Titles:@[@"手机号",@"验证码",@"新密码",@"确认新密码"] type:ByUserPwd ImageAry:nil];
 
    }
    return _userView;
}

-(UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
        
        UIButton *registBtn = [[UIButton alloc] initWithFrame:CGRectMake(20,20, SCREEN_WIDTH - 40, 40)];
        [registBtn setTitle:@"重置密码" forState:UIControlStateNormal];
        registBtn.backgroundColor = BY_THEME_COLOR;
        registBtn.layer.cornerRadius = 4.0;
        [registBtn addTarget:self action:@selector(resetPwdAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:registBtn];
    }
    return _footerView;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"loginCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];

    }
    [cell addSubview:self.userView];
    __weak typeof(self) weakself = self;
    self.userView.clickCodeBlock = ^(UIButton *btn){
        weakself.getCodeBtn = btn;
        [weakself getcode:btn];
    };
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return LineHeight * 4 + 120 + 4* 30;
}

#pragma mark -- 相关事件
// 重置密码
- (void)resetPwdAction:(UIButton *)btn{
//    [ByCoreToolAlert showOnlyText:@"重置密码"];
    UITextField *nameTf = [self.userView viewWithTag:10];
    UITextField *codeTf = [self.userView viewWithTag:11];
    UITextField *passTf = [self.userView viewWithTag:12];
    UITextField *conTf = [self.userView viewWithTag:13];
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    
    if (nameTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];
        return;
    }
    if (codeTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入验证码"];
        return;
    }
    if (passTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入密码"];
        return;
    }
    
    if (conTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请再次确认密码"];
        return;
    }
    
    if (passTf.text.length < 6 ) {
        [ByCoreToolAlert showOnlyText:@"密码长度至少6位"];
        return;
    }
    
    if (![passTf.text isEqualToString:conTf.text]) {
        [ByCoreToolAlert showOnlyText:@"两次输入密码不一致"];
        return;
    }
    model.username = nameTf.text;
    model.code = codeTf.text;
    model.password = passTf.text;

    [ByApiUser retrievePwd:model success:^(ByNetBaseResult *result) {
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:passTf.text,@"psw",nameTf.text,@"username", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"registermodel" object:nil userInfo:dic];
        [ByCoreToolAlert showOnlyText:@"重置密码"];
        [self performSelector:@selector(popVC) withObject:nil afterDelay:1];

    } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)popVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- 获取验证码
- (void)getcode:(UIButton *)btn{
    UITextField *nameTF = [self.userView viewWithTag:10];
    if (nameTF.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];
        return;
    }
    
    if (nameTF.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确手机号"];
        return;
    }
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.mobile = nameTF.text;
    model.type = @"2";
    btn.enabled = NO;
    [ByApiUser getCode:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showSuccessWithText:result.data];
        // 再次获取验证码定时器倒计时60秒
        _countdownTime = 60;
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdownAction:) userInfo:nil repeats:YES];
        [timer fire];
    } failure:^(ByNetBaseResult *result) {
        btn.enabled = YES;
        [ByCoreToolAlert showFailureWithText:result.msg];
        
    }];
    
}

#pragma mark -- 倒计时
- (void)countdownAction:(NSTimer *)timer{
    if (_countdownTime > 0) {
        _countdownTime --;
        [self.getCodeBtn setTitle:[NSString stringWithFormat:@"(%li)重新获取",(long)_countdownTime] forState:UIControlStateNormal];
    }else {
        self.getCodeBtn.enabled = YES;
        [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [timer invalidate];
    }
}

@end
